﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TemplatesPrint.DBHelper;
using TemplatesPrint.Entitys;

namespace TemplatesPrint.Views
{
    public partial class FrmCreateSys : UIEditForm
    {
        SchemeAmountEntity _schemeEntity = null;
        public FrmCreateSys(SchemeAmountEntity schemeAmountEntity = null)
        {
            InitializeComponent();
            _schemeEntity = schemeAmountEntity;
        }

        private void FrmCreateSys_Load(object sender, EventArgs e)
        {
            LoadInfo();
            this.ButtonOkClick += FrmCreateSys_ButtonOkClick;
        }

        void LoadInfo()
        {
            if (_schemeEntity!=null)
            {
                this.Text = "编辑";
                LoadEditInfo();
            }
        }

        void LoadEditInfo()
        {
            edtFangAn.Text = _schemeEntity.JoiningScheme;
            edtAmount.Text = _schemeEntity.Amount.ToString();
        }


        private void FrmCreateSys_ButtonOkClick(object sender, EventArgs e)
        {
            using (var db = SQLiteHelper.GetInstance())
            {
                SchemeAmountEntity schemeEntity = new SchemeAmountEntity();
                schemeEntity.JoiningScheme = edtFangAn.Text.Trim();
                schemeEntity.Amount = decimal.Parse( edtAmount.Text.Trim());


                if (_schemeEntity != null)
                {
                    schemeEntity.Id = _schemeEntity.Id;
                    db.Updateable(schemeEntity).ExecuteCommand();
                }
                else
                {
                    db.Insertable(schemeEntity).ExecuteCommand();
                }
            }
            this.Close();
        }

        protected override bool CheckData()
        {
            if (!decimal.TryParse(edtAmount.Text.Trim(), out var o))
            {
                MessageBox.Show("请输入正确的金额格式") ;
                return false;
            }
            return 
                    CheckEmpty(edtFangAn, "请选择加盟方案");
                 
        }
    }
}
